<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Complain;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ComplainController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Complain(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('agent_id');
            // $grid->column('code');
            $grid->column('ip');
            $grid->column('ua');
            $grid->column('openid');
            $grid->column('forbiden')->switch();
            $grid->column('created_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('ip');
                $filter->like('openid');
            });
            $grid->disableCreateButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableEdit();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Complain(), function (Show $show) {
            $show->field('id');
            $show->field('agent_id');
            $show->field('code');
            $show->field('ip');
            $show->field('openid');
            $show->field('ua');
            $show->field('url');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Complain(), function (Form $form) {
            $form->display('id');
            $form->text('agent_id');
            $form->text('code');
            $form->text('ip');
            $form->text('openid');
            $form->text('ua');
            $form->text('url');
            $form->switch('forbiden');
            $form->display('created_at');
            $form->display('updated_at');

            //编辑缓存同步
            $form->saved(function(Form $form){
                $openid = ($form->model()->openid);
                cache()->forget("bad_openid:{$openid}");
            });

            $form->deleted(function (Form $form, $result) {
                // 获取待删除行数据，这里获取的是一个二维数组
                $data = $form->model()->toArray();
                foreach($data as $item){
                    cache()->forget("bad_openid:{$item['openid']}");
                }
                
                // 通过 $result 可以判断数据是否删除成功
                if ( $result) {
                    
                    // 返回删除成功提醒，此处跳转参数无效
                    return $form->response()->success('删除成功');
                }
                return $form->response()->error('数据删除失败');
            });
        });
    }
}
